/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/vab/layouts'
import { publicPath, routerMode } from '@/config'

Vue.use(VueRouter)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true,
  },
  {
    path: '/power',
    component: () => import('@/views/login/power'),
    hidden: true,
  },
  {
    path: '/interact',
    component: () => import('@/views/login/interact'),
    hidden: true,
  },
  {
    path: '/shop',
    component: () => import('@/views/login/shop'),
    hidden: true,
  },
  {
    path: '/callback',
    component: () => import('@/views/callback'),
    hidden: true,
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/403'),
    hidden: true,
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    hidden: true,
  },
]

export const asyncRoutes = [
  // 传媒/地产
  {
    path: '/',
    name: 'Root',
    component: Layout,
    redirect: '/dashboard',
    meta: {
      title: '',
      icon: 'apps-2-line',
    },
    children: [
      /*// 权限首页
      {
        key: 9,
        path: '/',
        name: 'power',
        component: () => import('@/views/index'),
        meta: {
          title: '首页',
          icon: 'home-2-line',
          affix: true,
          roles: ['Index'],
        },
      },*/
      // 首页
      {
        key: 10,
        path: '/',
        name: 'Index',
        component: () => import('@/views/index'),
        meta: {
          title: '首页',
          icon: 'home-2-line',
          affix: true,
          roles: ['Index'],
        },
      },

      //region 地产端路由
      //项目管理
      {
        key: 18,
        path: '/projectManage',
        name: 'ProjectManage',
        component: () => import('@/views/projectManage'),
        redirect: '/project',
        meta: {
          title: '项目管理',
          icon: 'calendar-line',
          roles: ['projectManage'],
        },
        children: [
          // 项目列表列表
          {
            key: 1801,
            path: '/project',
            name: 'Project',
            component: () => import('@/views/dc_projectList'),
            meta: {
              title: '项目列表',
              roles: ['Project'],
            },
          },
          // 费用
          {
            path: '/projectExpenses',
            name: 'ProjectExpenses',
            component: () => import('@/views/dc_expenses'),
            hidden: true,
            meta: {
              title: '结算信息',
              activeMenu: '/project',
              roles: ['Project'],
            },
          },
          // 地产费用日志
          {
            path: '/projectCostLog',
            name: 'ProjectCostLog',
            component: () => import('@/views/dc_costLog'),
            hidden: true,
            meta: {
              title: '费用日志',
              activeMenu: '/project',
              roles: ['Project'],
            },
          },
          // 服务商管理
          {
            key: 1802,
            path: '/freelancer',
            name: 'Freelancer',
            component: () => import('@/views/dc_freelancer'),
            meta: {
              title: '服务商管理',
              roles: ['freelancer'],
            },
          },
          // 服务商类别添加
          {
            key: 1803,
            path: '/freelancerType',
            name: 'freelancerType',
            component: () => import('@/views/dc_freelancerType'),
            meta: {
              title: '服务商类别管理',
              roles: ['freelancerType'],
            },
          },
          // 大额收款清单
          {
            key: 1804,
            path: '/raleList',
            name: 'RaleList',
            component: () => import('@/views/raleList'),
            meta: {
              title: '大额收款清单',
              roles: ['RaleList'],
            },
          },
        ],
      },
      //付款管理
      {
        key: 19,
        path: '/dcPayment',
        name: 'dcPayment',
        component: () => import('@/views/dc_payment'),
        redirect: '/dcPaymentPending',
        meta: {
          title: '付款管理',
          icon: 'money-cny-box-line',
          roles: ['DcPayment'],
        },
        children: [
          {
            key: 1901,
            path: '/dcPaymentPending',
            name: 'dcPaymentPending',
            component: () => import('@/views/dc_payment/pending'),
            meta: {
              title: '付款申请',
              roles: ['DcPaymentPending'],
            },
          },
          {
            key: 1904,
            path: '/dcPaymentCheck',
            name: 'DcPaymentCheck',
            component: () => import('@/views/dc_payment/check'),
            meta: {
              title: '付款审核',
              roles: ['DcPaymentCheck'],
            },
          },
          {
            key: 1902,
            path: '/dcPaymentRequest',
            name: 'dcPaymentRequest',
            component: () => import('@/views/dc_payment/request'),
            meta: {
              title: '付款查询',
              roles: ['DcPaymentRequest'],
            },
          },
          {
            key: 1903,
            path: 'dcPayable',
            name: 'DcPayable',
            component: () => import('@/views/dc_payment/payable'),
            meta: {
              title: '应付查询',
              roles: ['DcPayable'],
            },
          },
          {
            key: 1905,
            path: 'billQuery',
            name: 'billQuery',
            component: () => import('@/views/dc_payment/billQuery'),
            meta: {
              title: '充值查询',
              roles: ['billQuery'],
            },
          },
          {
            key: 1906,
            path: 'dcCouponRecord',
            name: 'dcCouponRecord',
            component: () => import('@/views/dc_payment/couponRecord'),
            meta: {
              title: '优惠券记录',
              roles: ['dcCouponRecord'],
            },
          },
        ],
      },
      // 开票管理
      {
        key: 16,
        path: '/invoice',
        name: 'Invoice',
        component: () => import('@/views/invoice'),
        redirect: '/invoicePending',
        meta: {
          title: '开票管理',
          icon: 'bill-line',
          roles: ['DcInvoice'],
        },
        children: [
          {
            key: 1601,
            path: '/dcInvoicePending',
            name: 'DcInvoicePending',
            component: () => import('@/views/invoice/pending'),
            meta: {
              title: '开票申请',
              roles: ['DcInvoicePending'],
            },
          },
          {
            key: 1602,
            path: '/invoiceRequest',
            name: 'InvoiceRequest',
            component: () => import('@/views/invoice/request'),
            meta: {
              title: '开票查询',
              roles: ['DcInvoiceRequest'],
            },
          },
        ],
      },
      // 账号管理
      {
        key: 17,
        path: '/account',
        name: 'Account',
        component: () => import('@/views/account'),
        redirect: '/manager',
        meta: {
          title: '账号管理',
          icon: 'user-line',
          roles: ['DcAccount'],
        },
        children: [
          // 活动管理员
          {
            key: 1701,
            path: '/manager',
            name: 'Manager',
            component: () => import('@/views/manager'),
            meta: {
              title: '账号列表',
              roles: ['DcManager'],
            },
          },
          // 活动角色
          {
            key: 1702,
            path: '/role',
            name: 'Role',
            component: () => import('@/views/role'),
            meta: {
              title: '角色管理',
              roles: ['DcRole'],
            },
          },
        ],
      },
      //endregion

      //region 活动端路由
      // 活动管理
      {
        key: 12,
        path: '/event',
        name: 'Event',
        component: () => import('@/views/event'),
        redirect: '/activity',
        meta: {
          title: '项目管理',
          icon: 'calendar-line',
          roles: ['Event'],
        },
        children: [
          // 活动列表
          {
            key: 1201,
            path: '/activity',
            name: 'Activity',
            component: () => import('@/views/activity'),
            meta: {
              title: '项目列表',
              roles: ['Activity'],
            },
          },
          // 费用
          {
            path: '/activityExpenses',
            name: 'ActivityExpenses',
            component: () => import('@/views/activityExpenses'),
            hidden: true,
            meta: {
              title: '费用管理',
              activeMenu: '/activity',
              roles: ['Activity'],
            },
          },
          // 费用日志
          {
            path: '/activityCostLog',
            name: 'ActivityCostLog',
            component: () => import('@/views/activityCostLog'),
            hidden: true,
            meta: {
              title: '费用日志',
              activeMenu: '/activity',
              roles: ['Activity'],
            },
          },
          // 活动供应商
          {
            key: 1202,
            path: '/supplier',
            name: 'Supplier',
            component: () => import('@/views/supplier'),
            meta: {
              title: '供应商管理',
              roles: ['Supplier'],
            },
          },
          // 供应商类别添加
          {
            path: '/supplierCategory',
            name: 'SupplierCategory',
            component: () => import('@/views/supplierCategory'),
            meta: {
              title: '供应商类别添加',
              roles: ['SupplierCategory'],
            },
          },
          // 大额收款清单
          {
            key: 1204,
            path: '/raleList',
            name: 'RaleList',
            component: () => import('@/views/raleList'),
            meta: {
              title: '大额收款清单',
              roles: ['RaleList'],
            },
          },
        ],
      },
      // 付款管理
      {
        key: 13,
        path: '/payment',
        name: 'Payment',
        component: () => import('@/views/payment'),
        redirect: '/paymentPending',
        meta: {
          title: '付款管理',
          icon: 'money-cny-box-line',
          roles: ['Payment'],
        },
        children: [
          {
            key: 1301,
            path: '/paymentPending',
            name: 'PaymentPending',
            component: () => import('@/views/payment/pending'),
            meta: {
              title: '付款申请',
              roles: ['PaymentPending'],
            },
          },
          {
            key: 1304,
            path: '/paymentCheck',
            name: 'PaymentCheck',
            component: () => import('@/views/payment/check'),
            meta: {
              title: '付款审核',
              roles: ['PaymentCheck'],
            },
          },
          {
            key: 1302,
            path: '/paymentRequest',
            name: 'PaymentRequest',
            component: () => import('@/views/payment/request'),
            meta: {
              title: '付款查询',
              roles: ['PaymentRequest'],
            },
          },
          {
            key: 1303,
            path: 'payable',
            name: 'Payable',
            component: () => import('@/views/payment/payable'),
            meta: {
              title: '应付查询',
              roles: ['Payable'],
            },
          },
          {
            key: 1305,
            path: 'billQuery',
            name: 'billQuery',
            component: () => import('@/views/dc_payment/billQuery'),
            meta: {
              title: '充值查询',
              roles: ['billQuery'],
            },
          },
          {
            key: 1306,
            path: 'dcCouponRecord',
            name: 'dcCouponRecord',
            component: () => import('@/views/dc_payment/couponRecord'),
            meta: {
              title: '优惠券记录',
              roles: ['dcCouponRecord'],
            },
          },
        ],
      },
      // 开票管理
      {
        key: 14,
        path: '/invoice',
        name: 'Invoice',
        component: () => import('@/views/invoice'),
        redirect: '/invoicePending',
        meta: {
          title: '开票管理',
          icon: 'bill-line',
          roles: ['Invoice'],
        },
        children: [
          {
            key: 1401,
            path: '/invoicePending',
            name: 'InvoicePending',
            component: () => import('@/views/invoice/pending'),
            meta: {
              title: '开票申请',
              roles: ['InvoicePending'],
            },
          },
          {
            key: 1402,
            path: '/invoiceRequest',
            name: 'InvoiceRequest',
            component: () => import('@/views/invoice/request'),
            meta: {
              title: '开票查询',
              roles: ['InvoiceRequest'],
            },
          },
        ],
      },
      // 账号管理
      {
        key: 11,
        path: '/account',
        name: 'Account',
        component: () => import('@/views/account'),
        redirect: '/manager',
        meta: {
          title: '账号管理',
          icon: 'user-line',
          roles: ['Account'],
        },
        children: [
          // 活动管理员
          {
            key: 1101,
            path: '/manager',
            name: 'Manager',
            component: () => import('@/views/manager'),
            meta: {
              title: '账号列表',
              roles: ['Manager'],
            },
          },
          // 活动角色
          {
            key: 1102,
            path: '/role',
            name: 'Role',
            component: () => import('@/views/role'),
            meta: {
              title: '角色管理',
              roles: ['Role'],
            },
          },
        ],
      },
      //endregion

      /* // 地产项目
       {
         path: '/dcProject',
         name: 'DcProject',
         component: () => import('@/views/project'),
         meta: {
           title: '地产项目',
           roles: ['property'],
           icon: 'building-line',
         },
       },
       // 地产服务商
       {
         path: '/serviceProvider',
         name: 'ServiceProvider',
         component: () => import('@/views/serviceProvider'),
         meta: {
           title: '服务商',
           roles: ['property'],
           icon: 'service-line',
         },
       },*/
    ],
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true,
  },
]

const router = createRouter()

export function resetRouter(routes = constantRoutes) {
  router.matcher = createRouter(routes).matcher
}

function createRouter(routes = constantRoutes) {
  return new VueRouter({
    base: publicPath,
    mode: routerMode,
    scrollBehavior: () => ({
      y: 0,
    }),
    routes: routes,
  })
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}

export default router
